/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: Hua YY
 * Date: 2025-01-07
 * Time: 13:26
 */
class Solution6 {
    public int strStr(String haystack, String needle) {
        String str1 = haystack, str2 = needle;
        char ch = str2.charAt(0);
        char[] s1 = str1.toCharArray();
        int n = s1.length, m = str2.length() , ret = -1;
        if (n < m) return -1;
        boolean sign = false;
        for (int i = 0; i <= n - m;) {
            if (s1[i] != ch) i++;
            if (i == n - m) {//单独处理一下最后长度一样的字符串n-m是剩下的长度上限，下标上限就是n-m这种情况单独处理
                sign = str1.substring(i).equals(str2);
            } else if (i < n - m) {
                sign = str1.substring(i, i + m).equals(str2);
            }
            if (sign == true) {
                ret = i;
                break;
            } else {
                i++;
            }
        }
        return ret;

    }
}
public class Test6 {
}
